//
//  loginVC.m
//  Movie
//
//  Created by cjy on 16/4/8.
//  Copyright © 2016年 YJC. All rights reserved.
//

#import "loginVC.h"
#import <AVOSCloud/AVOSCloud.h>
#import "MeViewController.h"
#import "ZCViewController.h"
#import "PersonViewController.h"
#import "UIView+Twinkle.h"

#import <UIImage+GIF.h>

@interface loginVC ()
@property (nonatomic, strong) UIImageView *img;
@property (nonatomic, strong) UIImageView *loadingImageView;
@property (nonatomic, strong) UIButton *btnForZC;
@property (nonatomic, strong) UIButton *btnForDL;
@property (nonatomic, strong) UIButton *btnForReturn;
@property (nonatomic, strong) UILabel *labelFlash;
@property (nonatomic, assign) NSInteger count;


@end

@implementation loginVC


- (void)viewWillAppear:(BOOL)animated{

    NSNotificationCenter *notification = [NSNotificationCenter defaultCenter];
    [notification addObserver:self selector:@selector(dismissSelf:) name:@"dismiss" object:nil];
}


- (void)viewWillDisappear:(BOOL)animated{

    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"dismiss" object:nil];


}
- (void)dismissSelf:(NSNotificationCenter *)notification{
    
    [self dismissViewControllerAnimated:YES completion:^{
       
        [[NSNotificationCenter defaultCenter]postNotificationName:@"dismiss" object:self];

        
    }];


}
- (void)viewDidLoad {
    [super viewDidLoad];


    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor colorWithRed:0.141 green:0.039 blue:0.263 alpha:1.000];
    //    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];  // 隐藏文字
    
//    self.labelFlash = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 70)];

    [self create];
    [self initLoadingImageView];

}



// 加载动态图片
- (void)initLoadingImageView{
    
    NSString  *name = @"juju.gif";
    
    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
    
    NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
    
    
    if (!self.loadingImageView) {
        
        self.loadingImageView = [[UIImageView alloc]init];
        
    }
    
    //    imgView.image = [UIImage animatedImageWithAnimatedGIFData:imageData];
    
    self.loadingImageView.backgroundColor = [UIColor clearColor];
    
    self.loadingImageView.image = [UIImage sd_animatedGIFWithData:imageData];
    
    self.loadingImageView.frame = CGRectMake(80, 80, 110, 50);
    self.loadingImageView.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 3 - 60);
    
    [self.img addSubview:self.loadingImageView];
    
    [self.img bringSubviewToFront:self.loadingImageView];
    
}


- (void)create{

    self.img = [[UIImageView alloc] initWithFrame:self.view.frame];
    self.img.image = [UIImage imageNamed:@"h"];
    self.img.userInteractionEnabled = YES;
    
//    self.labelFlash.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 3 - 60);
//    self.labelFlash.text = @"JuJu";
//    self.labelFlash.font = [UIFont fontWithName:@"AvenirNext-Regular" size:32];
//    self.labelFlash.textColor = [UIColor colorWithRed:0.593 green:0.683 blue:1.000 alpha:1.000];
//    self.labelFlash.textAlignment = NSTextAlignmentCenter;
//    [self.labelFlash twinkle];
    
    
    self.userText = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 4 + 23, self.view.frame.size.height / 3 - 10, self.view.frame.size.width / 3 * 2, 35)];
    self.userText.backgroundColor = [UIColor colorWithRed:0 / 255.f green:0 / 255.f blue:0 / 255.f alpha:0.4];
    self.userText.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 3 + 10);
    self.userText.textColor = [UIColor whiteColor];
    self.userText.font = [UIFont boldSystemFontOfSize:16];
    self.userText.placeholder = @"请输入用户名: ";
    [self.userText setValue:[UIColor colorWithRed:0.807 green:0.829 blue:0.820 alpha:1.000] forKeyPath:@"_placeholderLabel.textColor"];
    [self.userText setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    self.userText.layer.cornerRadius = 2.f;
    self.userText.layer.masksToBounds = YES;
    self.userText.keyboardAppearance = UIKeyboardAppearanceDark;
    
    
    self.mimaText = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 4 + 20, self.view.frame.size.height / 3 + 30, self.view.frame.size.width / 3 * 2, 35)];
    self.mimaText.backgroundColor = [UIColor colorWithRed:0 / 255.f green:0 / 255.f blue:0 / 255.f alpha:0.4];
    self.mimaText.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 3 + 55);
    self.mimaText.tintColor = [UIColor whiteColor];
    self.mimaText.placeholder = @"请输入密码: ";     //
    [self.mimaText setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.mimaText setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    self.mimaText.layer.cornerRadius = 2.f;
    self.mimaText.layer.masksToBounds = YES;
    self.mimaText.hidden = NO;
    self.mimaText.keyboardAppearance = UIKeyboardAppearanceDark;
    self.mimaText.secureTextEntry = YES;
    self.mimaText.textColor = [UIColor whiteColor];
    self.mimaText.font = [UIFont boldSystemFontOfSize:16];
    
    
    self.btnForReturn = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 40, 40)];
    [self.btnForReturn setTitle:@"返回" forState:UIControlStateNormal];
    self.btnForReturn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.btnForReturn setTitleColor:[UIColor colorWithRed:100 / 255.f green:160 / 255.f blue:155 / 255.f alpha:1] forState:UIControlStateNormal];
    [self.btnForReturn addTarget:self action:@selector(returnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btnForDL = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btnForDL.frame = CGRectMake(self.view.frame.size.width / 4 + 20,self.view.frame.size.height / 2, self.view.frame.size.width / 3 - 15, 35);
    self.btnForDL.center = CGPointMake(self.view.center.x - 60, self.view.frame.size.height / 2 + 45);
    [self.btnForDL setTitle:@"登录" forState:UIControlStateNormal];
    self.btnForDL.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    self.btnForDL.layer.borderWidth = 1.0f;    // 外边框
    self.btnForDL.layer.cornerRadius = 6.f;
    self.btnForDL.layer.masksToBounds = YES;
    self.btnForDL.layer.borderColor = [UIColor colorWithRed:0.811 green:0.965 blue:0.876 alpha:1.000].CGColor;
    [self.btnForDL addTarget:self action:@selector(btnForDLAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btnForZC = [[UIButton alloc]initWithFrame:
                     CGRectMake(self.view.frame.size.width / 4 + 20,self.view.frame.size.height / 2, self.view.frame.size.width / 3 - 15, 35)];
    self.btnForZC.center = CGPointMake(self.view.center.x + 60, self.view.frame.size.height / 2 + 45);
    [self.btnForZC setTitle:@"注册" forState:UIControlStateNormal];
    self.btnForZC.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    self.btnForZC.layer.shadowColor = [UIColor whiteColor].CGColor;
    self.btnForZC.layer.borderWidth = 1.0f;    // 外边框
    self.btnForZC.layer.cornerRadius = 6.f;
    self.btnForZC.layer.masksToBounds = YES;
    self.btnForZC.layer.borderColor = [UIColor colorWithRed:0.811 green:0.965 blue:0.876 alpha:1.000].CGColor;
    [self.btnForZC addTarget:self action:@selector(btnForZCAction:) forControlEvents:UIControlEventTouchUpInside];
    

    [self.view addSubview:self.img];
    [self.img addSubview:self.userText];
    [self.img addSubview:self.mimaText];
    [self.img addSubview:self.btnForZC];
    [self.img addSubview:self.btnForDL];
    [self.img addSubview:self.btnForReturn];
    [self.img addSubview:self.labelFlash];
    [self.labelFlash twinkle];
}


- (void)returnAction:(UIButton *)btn{

    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)btnForZCAction:(UIButton *)btn{
    
    ZCViewController *zc = [[ZCViewController alloc]init];
    
    [self presentViewController:zc animated:YES completion:nil];
}


// 登录
- (void)btnForDLAction:(UIButton *)btn{
    
    [AVUser logInWithUsernameInBackground:self.userText.text password:self.mimaText.text block:^(AVUser *user, NSError *error) {
        if ([AVUser currentUser]) {
            
            NSLog(@"ddd");
            PersonViewController *person = [[PersonViewController alloc]init];
            [self presentViewController:person animated:YES completion:nil];
            

        } else if([self.userText.text isEqualToString:@""] && ![self.mimaText.text isEqualToString:@""]){
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"用户名为空!" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alert addAction:defaultAction];
            
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        } else if (![self.userText.text isEqualToString:@""] && [self.mimaText.text isEqualToString:@""]){
        
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"密码为空!" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alert addAction:defaultAction];
            
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        } else{
        
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"请输入正确的登录信息" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alert addAction:defaultAction];
            
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }
    }];
  
}



// 点击空白区域,回收键盘
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    /**释放第一响应*/
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
